package com.wangyadong.hobby.controller;

import com.wangyadong.hobby.common.utils.BaseControllerConfig;
import com.wangyadong.hobby.common.utils.ConfigurableApplitionUtil;
import com.wangyadong.hobby.common.utils.EbUploadUtil;
import com.wangyadong.hobby.common.utils.HobbyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ThemeResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**

 */
@Controller
@RequestMapping("/index/")
public class IndexController extends BaseControllerConfig {

    @PostMapping("defaultImport")
    public Object defaultImport(HttpServletRequest request, MultipartFile myfil) {
        return HobbyResult.success(EbUploadUtil.compressionExcel(request, myfil));
    }

    /**
     * 文件上传系统
     */
    @PostMapping("uploadPic")
    public Object uploadPic(HttpServletRequest request, MultipartFile myfil) {
        return HobbyResult.success(EbUploadUtil.uploadPic(request, myfil, getDiskPath()));
    }


    /**
     * 文件下载
     *
     * @param response
     * @param id
     * @return
     * @throws IOException
     */
    @PostMapping("download")
    public void download(HttpServletResponse response, Long id) throws IOException {
        //default-excel-path
        EbUploadUtil.download(new File(ConfigurableApplitionUtil.getProperty("default-excel-path")), response);
    }

//    @Autowired
//    private ThemeResolver themeResolver;
    @GetMapping("testtemp")
    @ResponseBody
    public HobbyResult test(HttpServletRequest request, Long id) throws IOException {
        //default-excel-path
//        System.out.println(themeResolver.resolveThemeName(request));
        return HobbyResult.success(System.currentTimeMillis());
    }


}
